package org.jiamandan.com.strategy.impl.tram.cmd;

import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.jiamandan.com.constant.BizConstant;
import org.jiamandan.com.dto.ZDTimeDto;
import org.jiamandan.com.entity.RechargeGun;
import org.jiamandan.com.service.IRechargeGunService;
import org.jiamandan.com.strategy.impl.tram.TramBasicService;
import org.jiamandan.com.utils.CommonUtil;
import org.jiamandan.com.utils.ZDObjectUtil;
import org.springframework.stereotype.Service;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @author:Jmd

 * @see:
 * @创建日期：2022-12-01 16:50
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2022-12-01 16:50	         Jmd        	创建
 * @end
 */
@Service
@AllArgsConstructor
public class ZDIssueTimeService implements TramBasicService {

    private IRechargeGunService iRechargeGunService;

    @Override
    public void doExecute(byte[] data, Channel client) {
        ZDTimeDto dto = JSON.parseObject(CommonUtil.bytesToString(data), ZDTimeDto.class);
        RechargeGun gun = iRechargeGunService.getById(dto.getGunId());
        if (gun != null) {
            StringBuilder builder = new StringBuilder();
            /**
             * 1 年 year 1 0-99
             * 2 月 month 1 1-12
             * 3 日 day 1 1-31
             * 4 时 hour 1 0-23
             * 5 分 minute 1 0-59
             * 6 秒 second 1 0-59
             */
            builder.append(StringUtils.leftPad(dto.getYear() + "", 2, '0'));
            builder.append(StringUtils.leftPad(dto.getMonth() + "", 2, '0'));
            builder.append(StringUtils.leftPad(dto.getDay() + "", 2, '0'));
            builder.append(StringUtils.leftPad(dto.getHour() + "", 2, '0'));
            builder.append(StringUtils.leftPad(dto.getMinute() + "", 2, '0'));
            builder.append(StringUtils.leftPad(dto.getSecond() + "", 2, '0'));
            client.writeAndFlush(ZDObjectUtil.splicingMessage(builder.toString(), gun.getPileNo(), BizConstant.Cmd.CMD_111));
        }
    }
}
